package com.heima.wemedia.interceptor;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.utils.WmThreadLocalUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WmTokenInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //得到header中的信息
        String userId = request.getHeader("userId");
        if (StrUtil.isNotEmpty(userId)) {
            //把用户id存入threadloacl中
            WmUser wmUser = new WmUser();
            wmUser.setId(Convert.toInt(userId));
            WmThreadLocalUtil.setUser(wmUser);
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        WmThreadLocalUtil.clear();
    }
}

